package com.scheduler.utils.trace;

import org.apache.commons.lang3.StringUtils;


/**
 * 获取TraceID
 */
public class TraceUtil {

    public static final String LOCAL = "local";
    public static final String ARMS = "arms";
    public static String traceType = LOCAL;

    /**
     * 获取TraceId
     *
     * @return
     */
    public static String getTraceId() {
        String result;
        if (traceType.equals(ARMS)) {
            result = SelfArms.getTraceId();
        } else {
            result = SelfLocal.getTraceId();
        }

        return result;
    }

    public static String getOrCreateTraceId(String traceId) {
        String result;
        if (traceType.equals(ARMS)) {
            if (traceId != null) {
                result = SelfArms.startRpc(traceId);
            } else {
                result = SelfArms.getTraceId();
                if (StringUtils.isBlank(result)) {
                    result = SelfArms.startRpc(null);
                }
            }
        } else {
            if (traceId != null) {
                result = SelfLocal.startRpc(traceId);
            } else {
                result = SelfLocal.getTraceId();
                if (StringUtils.isBlank(result)) {
                    result = SelfLocal.startRpc(null);
                }
            }
        }

        return result;
    }

    public static String createTraceId() {
        String result;
        if (traceType.equals(ARMS)) {
            result = SelfArms.startRpc(null);
        } else {
            result = SelfLocal.startRpc(null);
        }
        return result;
    }

    public static void endTraceId() {
        if (traceType.equals(ARMS)) {
            SelfArms.endTraceId();
        } else {
            SelfLocal.endTraceId();
        }
    }

    private static class SelfLocal {

        private static String getTraceId() {
            LocalTrace.TraceParam traceParam = LocalTrace.get();
            if (traceParam == null) {
                return null;
            }
            return traceParam.getTraceId();
        }

        private static String startRpc(String traceId) {
            String result = traceId != null ? traceId : TraceIdGenerator.generate();
            LocalTrace.set(LocalTrace.TraceParam.builder().traceId(result).build());
            return getTraceId();
        }

        private static void endTraceId() {
            LocalTrace.clean();
        }
    }

    private static class SelfArms {

        private static String getTraceId() {
//            Span span = Tracer.builder().getSpan();
//
//            if (span != null) {
//                return span.getTraceId();
//            }
            return null;
        }

        private static String startRpc(String traceId) {
            return null;
//            Span span = Tracer.builder().getSpan();
//            if (span == null) {
//                span = new Span();
//            }
//            if (traceId != null && traceId.trim().length() > 0) {
//                span.setTraceId(traceId);
//                Tracer.builder().startRpc(span);
//            } else {
//                String traceIdInner = span.getTraceId();
//                if (traceIdInner == null || traceIdInner.trim().length() == 0) {
//                    // 解决启动本地定时任务时没有traceId的问题
//                    traceIdInner = TraceIdGenerator.generate();
//                    span.setTraceId(traceIdInner);
//                    Tracer.builder().startRpc(span);
//                }
//            }
//            return span.getTraceId();
        }

        private static void endTraceId() {
//            Tracer.builder().endRpc();
        }
    }

}

